package com.cyandpos.pad.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import androidx.appcompat.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.refund.contract.RefundContract;
import com.cyandpos.pad.refund.presenter.RefundPresenterImpl;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class RefundDialog extends BaseActivity implements RefundContract.View {
    @BindView(R.id.tv_dialog_title_name)
    TextView mTvTitleName;
    @BindView(R.id.et_oldsktno)
    EditText mEtOldsktno;
    @BindView(R.id.ll_oldktno)
    LinearLayout mLlOldktno;
    @BindView(R.id.et_ticketno)
    EditText mEtTicketno;
    @BindView(R.id.ll_ticketno)
    LinearLayout mLlTicketno;
    @BindView(R.id.btn_commit)
    Button mBtnCommit;
    @BindView(R.id.member_soft_keyboard)
    SelfNumKeyView sf_containt;
    private ProgressDialog dialog;
    private RefundContract.Presenter mRefundPresenter;
    private String managerNo;
    private boolean isAllOrder = true;  //是否整单退货
    private boolean isOrderNum = true;  //退货输入是订单号还是小票号
    @Override
    protected void initData() {
        dialog = new ProgressDialog(RefundDialog.this);
        dialog.setCancelable(false);
        managerNo = getIntent().getStringExtra("managerNo");
        dialog.setMessage("获取原小票信息 ....");
        mEtOldsktno.setText(ErpCommonData.sktno);
        mRefundPresenter = new RefundPresenterImpl(this,this);
        mTvTitleName.setText("退货");





        sf_containt.setEnterReceptor(mEtTicketno);
        sf_containt.setIsCanInputPoint(true);
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                mEtTicketno.setText(num);
                mRefundPresenter.getOldTicket(isOrderNum);

            }
        });

        mTvTitleName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        CommonUtils.setNoSoftware(mEtTicketno, RefundDialog.this, true);
//        mEtTicketno.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CommonUtils.hideSoftInputButWithSelection(mEtTicketno,RefundDialog.this);
//                mEtTicketno.setFocusable(true);
//                mEtTicketno.setFocusableInTouchMode(true);
//                mEtTicketno.requestFocus();
//                mRefundPresenter.getOldTicket(isOrderNum);
//            }
//        });
//
//
//        etDiscountRate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CommonUtils.hideSoftInputButWithSelection(etDiscountRate,activity);
//                etDiscountRate.setFocusable(true);
//                etDiscountRate.setFocusableInTouchMode(true);
//                etDiscountRate.requestFocus();
//            }
//        });


        setActivityDialogSize();
    }

    @OnCheckedChanged({R.id.rb_all,R.id.rb_select,R.id.rb_orderno,R.id.rb_jlbh})
    public void onRadioCheck(CompoundButton view,boolean ischanged){
        switch (view.getId()){
            case R.id.rb_all:
                if(ischanged){
                    isAllOrder = true;
                }
                break;
            case R.id.rb_select:
                if(ischanged){
                    isAllOrder = false;
                }
                break;
            case R.id.rb_orderno:
                if(ischanged){
                    isOrderNum = true;
                    mEtTicketno.setHint("请输入原订单号");
                }
                break;
            case R.id.rb_jlbh:
                if(ischanged){
                    isOrderNum = false;
                    mEtTicketno.setHint("请输入原小票号");
                }
                break;
        }

    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_refund;
    }

    @OnClick(R.id.btn_commit)
    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit: {
               mRefundPresenter.getOldTicket(isOrderNum);
            }
            break;
        }
    }

    @Override
    public void showProgress() {
        dialog.setMessage("正在获取原小票信息，请稍后 ....");
        dialog.show();
    }

    @Override
    public void dismissDialog() {
        if(dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @Override
    public void showErrMessage(String errorMsg) {
        ToastUtils.showMsg(errorMsg);
    }

    @Override
    public void onSucess(TicketInfo ticketInfo) {
        Intent intent = new Intent(this,RefundActivity.class);
        intent.putExtra("TicketInfo",ticketInfo);
        intent.putExtra("old_sktno",getOldSktNo());
        intent.putExtra("old_orderNo",getOldOrderNo());
        intent.putExtra("managerNo",managerNo);
        intent.putExtra("isAllOrder",isAllOrder);
        startActivity(intent);
    }

    @Override
    public String getOldSktNo() {
        return mEtOldsktno.getText().toString();
    }

    @Override
    public String getOldOrderNo() {
        return mEtTicketno.getText().toString();
    }

    private void setActivityDialogSize() {

        Display mDisplay = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        mDisplay.getMetrics(outMetrics);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = (int) (outMetrics.widthPixels * 0.5);
        //设置高度会导致弹框无法居中
        layoutParams.height = (int) (outMetrics.heightPixels * 0.5);
        getWindow().setAttributes(layoutParams);
    }
}
